package xyz.gzdxc.es.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.gzdxc.es.model.DemoModel;
import xyz.gzdxc.es.repository.ESDemoRepository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/testEs")
public class TestEsController {

    @Autowired
    ESDemoRepository eSDemoRepository;

    @RequestMapping("/testEsSave")
    public String testEsSave(HttpServletRequest request, HttpServletResponse response){
        DemoModel demoModel = new DemoModel();
        demoModel.setId("888");
        demoModel.setAppName("yh888");
        demoModel.setAppEnv("yhenv");
        eSDemoRepository.save(demoModel);
        return "success";
    }

    @RequestMapping("/testEsUpdate")
    public String testEsUpdate(HttpServletRequest request, HttpServletResponse response){
        DemoModel demoModel = new DemoModel();
        demoModel.setId("888");
        demoModel.setAppName("yh777");
        demoModel.setAppEnv("yhenv777");
        eSDemoRepository.save(demoModel);
        return "success";
    }

    @RequestMapping("/testEsDelete")
    public String testEsDelete(HttpServletRequest request, HttpServletResponse response){
        DemoModel demoModel = new DemoModel();
        demoModel.setId("888");
        eSDemoRepository.delete(demoModel);
        return "success";
    }

    @RequestMapping("/testEsFindAll")
    public String testEsFindAll(HttpServletRequest request, HttpServletResponse response){
        List<DemoModel> demoModelList = eSDemoRepository.findAll();
        System.out.println(demoModelList);
        return "success";
    }

    @RequestMapping("/testEsFindById")
    public String testEsFindById(HttpServletRequest request, HttpServletResponse response){
        Optional<DemoModel> demoModelOpt = eSDemoRepository.findById("888");
        DemoModel demoModel = demoModelOpt.get();
        System.out.println(demoModel);
        return "success";
    }

}
